package com.gzx.plugin.tjzy.modular.sms.template.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhs.trans.service.impl.DictionaryTransService;
import com.gzx.plugin.tjzy.modular.sms.template.bo.GzxSmsTemplateAddOrEditBo;
import com.gzx.plugin.tjzy.modular.sms.template.bo.GzxSmsTemplateQueryBo;
import com.gzx.plugin.tjzy.modular.sms.template.entity.GzxSmsTemplate;
import com.gzx.plugin.tjzy.modular.sms.template.mapper.GzxSmsTemplateMapper;
import com.gzx.plugin.tjzy.modular.sms.template.service.GzxSmsTemplateListService;
import com.gzx.plugin.tjzy.modular.sms.template.service.GzxSmsTemplateService;
import com.gzx.plugin.tjzy.modular.sms.template.vo.GzxSmsTemplateVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.consts.GzxDictConstant;
import vip.xiaonuo.common.tenant.TenantContext;
import vip.xiaonuo.common.util.MapstructUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zjt
 * @description
 * @date 2025/3/6 18:09
 */
@Slf4j
@Service
@DS("sms")
@RequiredArgsConstructor
public class GzxSmsTemplateServiceImpl implements GzxSmsTemplateService, InitializingBean {

    private final GzxSmsTemplateMapper smsTemplateMapper;

    private final GzxSmsTemplateListService smsTemplateListService;

    private final DictionaryTransService dictionaryTransService;

    /**
     * 短信模版分页查询
     *
     * @param smsTemplateQueryBo
     * @return
     */
    @Override
    public Page<GzxSmsTemplateVo> pageList(GzxSmsTemplateQueryBo smsTemplateQueryBo) {
        return smsTemplateMapper.selectVoPage(new Page<>(smsTemplateQueryBo.getCurrent(), smsTemplateQueryBo.getSize()),
                Wrappers.<GzxSmsTemplate>lambdaQuery()
                        .like(StringUtils.isNotBlank(smsTemplateQueryBo.getTemplateName()), GzxSmsTemplate::getTemplateName, smsTemplateQueryBo.getTemplateName())
                        .orderByDesc(GzxSmsTemplate::getId));
    }

    /**
     * 查询所有模版
     *
     * @return
     */
    @Override
    public List<GzxSmsTemplateVo> list() {
        return smsTemplateMapper.selectVoList();
    }

    /**
     * 短信模版新增或修改
     *
     * @param smsTemplateAddOrEditBo
     */
    @Override
    public void addOrEdit(GzxSmsTemplateAddOrEditBo smsTemplateAddOrEditBo) {
        GzxSmsTemplate smsTemplate = MapstructUtils.convert(smsTemplateAddOrEditBo, GzxSmsTemplate.class);
        if (smsTemplateAddOrEditBo.getId() == null) {
            smsTemplateMapper.insert(smsTemplate);
        } else {
            smsTemplateMapper.updateById(smsTemplate);
        }
        refreshCache();
    }

    /**
     * 删除短信模版
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        smsTemplateMapper.deleteById(id);
    }

    @Override
    public void afterPropertiesSet() {
        refreshCache();
    }

    private void refreshCache() {
        Thread.startVirtualThread(() -> {
            dictionaryTransService.makeUseRedis();
            try {
                TenantContext.disableTenantFilter();
                List<GzxSmsTemplate> smsTemplateList = smsTemplateListService.listAll();
                dictionaryTransService.refreshCache(GzxDictConstant.SMS_TEMPLATE, smsTemplateList.stream().collect(Collectors.toMap(GzxSmsTemplate::getTemplateCode, GzxSmsTemplate::getTemplateName)));
            } finally {
                TenantContext.enableTenantFilter();
                TenantContext.clear();
            }
        });
    }
}
